package com.m3839.sdk.login.model;

import com.m3839.sdk.common.GlobalManager;
import com.m3839.sdk.common.core.ApiError;
import com.m3839.sdk.common.core.Constant;
import com.m3839.sdk.common.env.EnvConfig;
import com.m3839.sdk.common.env2.ApiManager;
import com.m3839.sdk.common.event.SubjectBus;
import com.m3839.sdk.common.http.IHttpManager;
import com.m3839.sdk.common.http.listener.SimpleHttpRequestListener;
import com.m3839.sdk.common.util.LogUtils;
import com.m3839.sdk.login.bean.GameConfig;
import com.m3839.sdk.login.model.api.IInitModel;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

/**
 * @author ChenBaoyang
 * @description: InitApiModel
 * @date 2021/12/8 10:07
 */
public class InitApiModel implements IInitModel {

    private static final String TAG = "InitApiModel";

    private static final String INIT_PATH = "game/init";

    /**
     * SDK初始化url
     */
    private static final String INIT_BASE_URL = ApiManager.getInstance().buildYuanChuangUrl(INIT_PATH);


    @Override
    public void loadGameConfig() {
        Map<String, Object> param = ApiManager.getInstance().getCommonParam("0", INIT_PATH);
        IHttpManager httpManager = GlobalManager.getInstance().getHttpManager();
        EnvConfig config = ApiManager.getInstance().getYuanChuangApiEnvConfig();

        httpManager.requestPost(INIT_BASE_URL, param,
                ApiManager.getInstance().getCommonHeaders(config),
                new SimpleHttpRequestListener() {
                    @Override
                    public void onResult(String response) throws JSONException {
                        parseJsonData(response);
                    }

                    @Override
                    public void onError(ApiError apiError) {
                        LogUtils.e(TAG, apiError.toString());
                        SubjectBus.get().postValue(Constant.HYKB_EVENT_INIT_FAILED, apiError);
                    }
                });
    }

    /**
     * 解析响应数据
     * @param response 响应数据
     * @throws JSONException
     */
    private void parseJsonData(String response)
            throws JSONException {

        JSONObject data = new JSONObject(response);

        final int code = data.optInt(Constant.HYKB_KEY_RESPONSE_CODE);
        final String msg = data.optString(Constant.HYKB_KEY_RESPONSE_MSG);
        final JSONObject dataJson = data.optJSONObject(Constant.HYKB_KEY_RESPONSE_DATA);

        GameConfig config = GameConfig.parseJson(dataJson, code);

        switch (code) {
            case Constant.HYKB_CODE_SUCCESS:
                //初始化成功
                SubjectBus.get().postValue(Constant.HYKB_EVENT_INIT, config);
                break;
            case Constant.HYKB_CODE_GAME_MAINTENANCE:
                //返回初始化错误信息给游戏
                SubjectBus.get().postValue(Constant.HYKB_EVENT_MAINTENANCE, config);
                break;
            default:
                //返回初始化错误信息给游戏
                ApiError error = new ApiError(code, msg);
                SubjectBus.get().postValue(Constant.HYKB_EVENT_INIT_FAILED, error);
                break;
        }

    }
}
